home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / boot / tlpatch204.lha / tlpatch / V2.04 / Source / TLPatch204.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-25  |  9.0 KB  |  420 lines

  1. /* TLPatch.c V1.0                   */
  2. /* Translator.library patch utility */
  3. /* Richard Sheppard - Jan. 7, 1991  */
  4. /* Toronto, Canada                  */
  5. /* (source for Lattice C ??)        */
  6.  
  7. /* TLPatch204.c                     */
  8. /* translator.library patch utility */
  9. /* Kenneth Jennings - Dec. 23, 1994 */
  10. /* Miami, Florida                   */
  11. /* Source for SAS/C 6.51            */
  12.  
  13.  
  14. #include <exec/types.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <exec/execbase.h>
  18. #include <exec/nodes.h>
  19. #include <proto/exec.h>
  20.  
  21. void AddFile(void);
  22. void Validate(void);
  23. void Extract(void);
  24. int  GetLine(char line[], int max);
  25. int  GetExcept(char line[], int max);
  26. int  StrIndex(char source[], char searchfor[]);
  27.  
  28. FILE *fp1, *fp2;                    /* input file (exception table), outfile */
  29. FILE *fp3;                            /* input file (translator.library)       */
  30. #ifdef DEBUG_ON
  31. FILE *fp4;                            /* Debugging file                        */
  32. #endif
  33.  
  34. char pattern[] = "[A";                /* 1st search pattern                    */
  35. char line[256];                        /* line buffer                           */
  36. LONG codesize;                        /* size of code & xtbl hunk              */
  37. LONG jmptab[28];                    /* jump table array                      */
  38.  
  39. WORD code[1024];                    /* original code at start                */
  40. LONG reloc[45] =                    /* reloc table at end of file            */
  41. {
  42.     0x000003EC, 0x00000003,    0x00000000, 0x00000012,
  43.     0x0000000E, 0x00000002,    0x00000003, 0x00000000,
  44.     0x0000007E, 0x0000007A,    0x00000076, 0x00000012,
  45.     0x00000000, 0x000005DC,    0x000005D8, 0x000005D4,
  46.     0x000005D0, 0x000005CC,    0x000005C8, 0x000005C4,
  47.     0x000005C0, 0x000005BC,    0x000005B8, 0x000005B4,
  48.     0x000005B0, 0x000005AC,    0x00000574, 0x00000352,
  49.     0x0000033A, 0x0000019C,    0x00000132, 0x00000001,
  50.     0x00000000, 0x00000006,    0x00000001, 0x00000000,
  51.     0x00000016, 0x00000001, 0x00000000, 0x00000060,
  52.     0x00000000, 0x000003F2, 0x000003E9, 0x00000000,
  53.     0x000003F2
  54. };
  55.  
  56. struct ExecBase *ExecBase = NULL;
  57. struct Node *node;
  58.  
  59.  
  60.  
  61.  
  62. void main(int argc,char *argv[])
  63. {
  64.     int c,opt;
  65.     int x    = 0;
  66.     int xcnt = 0;
  67.     int size = 0;
  68.  
  69.     printf("\n\tTLPatch    - translator.library patch utility V1.0\n");
  70.     printf("\t               For WB 1.3, Rel. 33.2 library\n");
  71.     printf("\t               Richard Sheppard - 1991\n\n");
  72.  
  73.     printf("\n\tTLPatch204 - translator.library patch utility V2.04\n");
  74.     printf("\t               For WB 2.04, Rel. 37.1 library\n");
  75.     printf("\t               Kenneth Jennings - 1994\n\n");
  76.  
  77.     while (--argc > 0) 
  78.     {
  79.         if ( (*++argv)[0] == '-')    /* parse command line arguments */
  80.         {
  81.             while (opt = *++argv[0])
  82.             {
  83.                 switch (opt)
  84.                 {
  85.                     case 'x':
  86.                     case 'X':
  87.                         Extract();
  88.                         exit(0);
  89.                         break;
  90.  
  91.                     default:
  92.                         break;
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     if ( (fp1 = fopen("ram:except.tbl","r")) == NULL)
  99.     {
  100.         printf("*** Can't open ram:except.tbl\n");
  101.         printf("--- Copy your except.tbl to ram: or\n");
  102.         printf("--- use \"%s -x\" to extract table.\n",*argv);
  103.         exit(0);
  104.     }
  105.     else if ( (fp2 = fopen("ram:xtable.new","w")) == NULL)
  106.     {
  107.         printf("*** Can't open ram:xtable.new\n");
  108.         fclose(fp1);
  109.         exit(0);
  110.     }
  111.     else if ( (fp3 = fopen("sys:libs/translator.library","rb")) == NULL)
  112.     {
  113.         printf("*** Can't open sys:libs/translator.library\n");
  114.         fclose(fp2);
  115.         fclose(fp1);
  116.         exit(0);
  117.     }
  118. #ifdef DEBUG_ON
  119.     else if ( (fp4 = fopen("ram:tl.debug","w")) == NULL)
  120.     {
  121.         printf("*** Can't open ram:tl.debug\n");
  122.         fclose(fp3);
  123.         fclose(fp2);
  124.         fclose(fp1);
  125.         exit(0);
  126.     }
  127. #endif
  128.  
  129.     if ( !(ExecBase = (struct ExecBase *)OpenLibrary("exec.library", 0L)) )
  130.         printf("*** Can't open ExecBase\n");
  131.     else
  132.     {
  133.         if ( (node = (struct Node *)FindName( &ExecBase->LibList,
  134.                                               "translator.library") ) != 0)
  135.         {
  136.             printf("\tRemoving resident translator.library\n");
  137.             Remove(node);
  138.         }
  139.     }
  140.  
  141.     printf("\tReading exception table...\n");
  142. #ifdef DEBUG_ON
  143.     fprintf(fp4, "Reading exception table...\n");
  144. #endif
  145.  
  146.     while ( (c = GetLine(line, 256) ) > 0)
  147.     {
  148.         size = size + c;
  149. #ifdef DEBUG_ON
  150.         fprintf(fp4, "Read %d chars, size is now (%d, 0x%0X) bytes: %s\n", 
  151.                 c, size, size, line);
  152. #endif
  153.         if (StrIndex(line, pattern) >= 0)
  154.         {
  155.             jmptab[x] = (LONG)(size - c + 0x70);/* table size = 0x70 (112) Bytes */
  156. #ifdef DEBUG_ON
  157.             fprintf(fp4, "Calc jmptab[%d] = (%d, 0x%0X)\n", x, jmptab[x], jmptab[x]);
  158. #endif
  159.             x++;
  160.             pattern[1]++;                        /* increment search character */
  161.  
  162.             if(pattern[1] == 91)
  163.                 pattern[1] = 48;                /* "0"  - start of numbers */
  164.  
  165.             if(pattern[1] == 49)
  166.                 pattern[1] = 32;                /* " "  - start of punctuation */
  167.  
  168.             if(pattern[1] == 33)
  169.                 pattern[0] = 0;                    /* NULL */
  170.         }
  171.  
  172.         fprintf(fp2,"%s",line);                    /* write new xtable */
  173.         xcnt++;                                    /* exception count */
  174.     }
  175.  
  176.     fputc('\0',fp2);                            /* NULL at end of exception table */
  177.  
  178.     size++;                                        /* size + NULL */
  179. #ifdef DEBUG_ON
  180.     fprintf(fp4, "Wrote NULL, size is now (%d, 0x%0X) bytes, DeltaLONG %d\n", 
  181.             size, size, size%4 );
  182. #endif
  183.  
  184.     if (size % 4)
  185.     {
  186.         for (x = (size % 4); x < 4; x++)
  187.         {
  188.             fputc('\0', fp2);                    /* align table to LONG boundary */
  189.             size++;
  190. #ifdef DEBUG_ON
  191.             fprintf(fp4, "Filling NULL, size is now (%d, 0x%0X) bytes, DeltaLONG %d\n", 
  192.                         size, size, size%4);
  193. #endif
  194.         }
  195.     }
  196.  
  197.     codesize = (size + 0x77C) / 4;                /* 0x77C (1916) = code + jmptab - hunks */
  198. #ifdef DEBUG_ON
  199.     fprintf(fp4, "Calc codesize = (%d, 0x%0X) is (size (%d, 0x%0X) + (1916, 0x77C) ) / 4\n", 
  200.                         codesize, codesize, size, size);
  201. #endif
  202.  
  203.     fclose(fp2);
  204.     fclose(fp1);
  205.  
  206.     printf("\tReading original code...\n");
  207. #ifdef DEBUG_ON
  208.     fprintf(fp4, "Reading original code...\n");
  209. #endif
  210.  
  211.     Validate();
  212.  
  213.     fclose(fp3);
  214.  
  215.     if (codesize <= 0xFFFF)                        /* Hunks in 64K blocks */
  216.     {
  217.         code[10] = 0x0000;                        /* patch hunk size */
  218.         code[11] = (WORD)codesize;
  219.     }
  220.     else
  221.     {
  222.         code[10] = (WORD)codesize / 0xFFFF;        /* patch hunk size */
  223.         code[11] = (WORD)codesize % 0xFFFF;
  224.     }
  225.     code[16] = code[10];                        /* 2nd copy of hunk size */
  226.     code[17] = code[11];
  227. #ifdef DEBUG_ON
  228.     fprintf(fp4, "Calc Hunks [10/16] = 0x%0X, [11/17] = 0x%0X\n", 
  229.                 code[10], code[11]);
  230. #endif
  231.  
  232.     if ( (fp1 = fopen("ram:translator.library","wb")) == NULL)
  233.     {
  234.         printf("*** Can't create ram:translator.library\n");
  235.         exit(0);
  236.     }
  237.  
  238. #ifdef DEBUG_ON
  239.     fprintf(fp4, "Writing code and jumptable...\n");
  240. #endif
  241.     fwrite((char *)&code,2,0x398,fp1);    /* write code: 0x1CC L, 0x398 W, 0x730 (1840)B */
  242.     fwrite((char *)&jmptab,2,0x38,fp1);    /* write jmptbl: 0x1C L, 0x38 W, 0x70 (112)B   */
  243.  
  244. #ifdef DEBUG_ON
  245.     fprintf(fp4, "Appending new exceptions table...\n");
  246. #endif
  247.     AddFile();                            /* append new xtable ?? bytes                  */
  248.  
  249. #ifdef DEBUG_ON
  250.     fprintf(fp4, "Writing reloc code...\n");
  251. #endif
  252.     fwrite((char *)&reloc,2,0x5A,fp1);    /* write reloc: 0x2D L, 0x5A W, 0xB4 (180)B    */
  253.  
  254.     fclose(fp1);
  255.  
  256.     remove("ram:xtable.new");
  257.  
  258.     printf("\n\tThe new translator.library is now in ram:\n");
  259.     printf("\t\t\t%d exceptions\n\n",xcnt);
  260.  
  261.     if(ExecBase)
  262.         CloseLibrary((struct Library *)ExecBase);
  263.  
  264. #ifdef DEBUG_ON
  265.     fprintf(fp4, "Finished\n");
  266.     fclose(fp4);
  267. #endif
  268.  
  269.     exit(0);
  270. }
  271.  
  272.  
  273.  
  274. int GetLine(char line[], int max)
  275. {
  276.     int i = 0;
  277.     int c;
  278.  
  279.     while( (--max > 0) && ( (c = fgetc(fp1)) != EOF) && (c != '\n') )
  280.         line[i++] = c;
  281.  
  282.     line[i] = '\0';
  283.  
  284.     return i;
  285. }
  286.  
  287.  
  288. int GetExcept(char line[], int max)
  289. {
  290.     int i = 0;
  291.     int c;
  292.  
  293.     while ( (--max > 0)              && 
  294.             ( (c=fgetc(fp3)) != EOF) && 
  295.             (c != '\\')              && 
  296.             (c != '`') )
  297.         line[i++] = c;
  298.  
  299.     if (c == '\\' || c == '`')
  300.         line[i++] = c;
  301.  
  302.     line[i] = '\0';
  303.  
  304.     return i;
  305. }
  306.  
  307.  
  308. int StrIndex(char source[], char searchfor[])
  309. {
  310.     int i;
  311.  
  312.     for (i = 0; source[i] != '\0'; i++)
  313.     {
  314.         if ( (source[i]   == searchfor[0])  && 
  315.              (source[i+1] == searchfor[1]) )
  316.             return i;
  317.     }
  318.  
  319.     return -1;
  320. }
  321.  
  322.  
  323. void AddFile()
  324. {
  325.     int c;
  326. #ifdef DEBUG_ON
  327.     int cnt = 0;
  328. #endif
  329.  
  330.     if ( (fp2 = fopen("ram:xtable.new","r")) == NULL)
  331.     {
  332.         printf("*** Can't reead ram:xtable.new\n");
  333. #ifdef DEBUG_ON
  334.         fprintf(fp4, "*** Can't read ram:xtable.new\n");
  335.         fclose(fp4);
  336. #endif
  337.         fclose(fp1);
  338.         exit(0);
  339.     }
  340.  
  341.     while ( (c = getc(fp2)) != EOF)
  342.     {
  343. #ifdef DEBUG_ON
  344.         cnt++;
  345. #endif
  346.         putc(c,fp1);
  347.     }
  348.  
  349.     fclose(fp2);
  350.  
  351. #ifdef DEBUG_ON
  352.     fprintf(fp4, "Wrote (%d, 0x%0X) chars, DeltaWORD %d, DeltaLONG %d\n", 
  353.                 cnt, cnt, cnt%2, cnt%4);
  354. #endif
  355.  
  356.     return;
  357. }
  358.  
  359.  
  360. void Validate()
  361. {
  362.     int x;
  363.  
  364.     fread((char *)&code,2,0x398,fp3); /* read 0x398 words (thru byte 0x72F) */
  365.  
  366.     if ( (code[0x2e] != 0x3337) ||     /* Check words at byte location 0x5c, 0x5e */
  367.          (code[0x2f] != 0x2e31) )
  368.     {
  369.         printf("*** WARNING - not V37.1, unpredictable results could occur!\n");
  370.  
  371.         for(x = 0; x < 2; x++)
  372.             printf("%04x ", code[0x5c + x]);
  373.  
  374.         printf("\n");
  375.     }
  376.     return;
  377. }
  378.  
  379.  
  380. void Extract()
  381. {
  382.     int c;
  383.  
  384.     printf("\tExtracting exception table ...\n");
  385.  
  386.     if ( (fp1 = fopen("ram:except.tbl","w")) == NULL)
  387.     {
  388.         printf("*** Can't create ram:except.tbl\n");
  389.         exit(0);
  390.     }
  391.     else if ( (fp3 = fopen("sys:libs/translator.library","rb")) == NULL)
  392.     {
  393.         printf("*** Can't open sys:libs/translator.library\n");
  394.         fclose(fp1);
  395.  
  396.         exit(0);
  397.     }
  398.  
  399.     Validate();
  400.  
  401.     fread((char *)&code, 2, 0x38, fp3);    /* read jump table Bytes 0x730 thru 0x7af */
  402.  
  403.     while ( (c = GetExcept(line, 256)) > 0)
  404.     {
  405.         fprintf(fp1,"%s",line);
  406.  
  407.         if(line[1] != '\\' && line[1] != '`')
  408.             fprintf(fp1,"\n");
  409.     }
  410.  
  411.     fclose(fp3);
  412.     fclose(fp1);
  413.  
  414.     printf("\n\tThe exception table is now in ram:except.tbl\n\n");
  415.  
  416.     return;
  417. }
  418.  
  419. /* end of TLPatch204.c */
  420.